﻿define(function (require, exports, module) {
    var Common = require('common');

    //options:
    //  SX、SY、SW、SH、DX、DY、DW、DH请参考canvas.drawImage函数
    //  Src：图片地址
    function Image(options) {
        this.Options = $.extend({}, options);
        require('imageStorage').Get(this.Options.Src, this, function (self, img) {
            delete self.Options.Src;
            self.Options.Img = img;

            self.Options.hasOwnProperty('SX') || (self.Options.SX = 0);
            self.Options.hasOwnProperty('SY') || (self.Options.SY = 0);
            self.Options.hasOwnProperty('SW') || (self.Options.SW = img.width);
            self.Options.hasOwnProperty('SH') || (self.Options.SH = img.height);
            self.Options.hasOwnProperty('DW') || (self.Options.DW = img.width);
            self.Options.hasOwnProperty('DH') || (self.Options.DH = img.height);
        });
    };

    Image.prototype.Render = function () {
        this.Options.Canvas.Call('drawImage', this.Options.SX, this.Options.SY, this.Options.SW, this.Options.SH,
            this.Options.DX, this.Options.DY, this.Options.DW, this.Options.DH);
    };

    Image.prototype.IsOver = function (x, y) {
        return Common.IsInRect({
            X: x,
            Y: y
        }, {
            Left: this.Options.DX,
            Top: this.Options.DY,
            Right: this.Options.DX + this.Options.DW,
            Bottom: this.Options.DY + this.Options.DH
        });
    };

    Image.prototype.Handle = function (eventName, x, y) {
        Common.IsInRect({
            X: x,
            Y: y
        }, {
            Left: this.Options.DX,
            Top: this.Options.DY,
            Right: this.Options.DX + this.Options.DW,
            Bottom: this.Options.DY + this.Options.DH
        }) && this.Options.hasOwnProperty(eventName) && this.Options[eventName](x, y);
    };
});